import 'dart:collection';
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:time/data/version_bean.dart';
import 'package:time/dio/dir_http.dart';
import 'package:time/http/callback/http_request_callback.dart';
import 'package:time/http/http_request.dart';

class HttpApp extends StatelessWidget {
  const HttpApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "网络请求",
      theme: ThemeData(primarySwatch: Colors.cyan),
      home: const HttpPage(),
    );
  }
}

class HttpPage extends StatefulWidget {
  const HttpPage({Key? key}) : super(key: key);

  @override
  State<HttpPage> createState() {
    return HttpPageState();
  }
}

class HttpPageState extends State<HttpPage> {
  var downloadProgress = "下载";
  var uploadProgress = "上传";
  CancelToken? cancelToken;
  CancelToken? cancelTokenUpload;
  var getContent = "";
  String? downloadUrl = "";
  DioHttp dioHttp = DioHttp();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        leading: IconButton(
          onPressed: () {
            Navigator.pop(context);
          },
          icon: const Icon(Icons.arrow_back),
        ),
        title: const Text(
          "网络请求",
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: Column(
        children: [
          MaterialButton(
            color: Colors.indigo,
            child: const Text(
              "get请求",
              style: TextStyle(color: Colors.white),
            ),
            onPressed: () => request(),
          ),
          Text(getContent),
          MaterialButton(
            color: Colors.indigo,
            child: Text(
              downloadProgress,
              style: const TextStyle(color: Colors.white),
            ),
            onPressed: () => download(),
          ),
          MaterialButton(
            color: Colors.indigo,
            child: const Text(
              "停止",
              style: TextStyle(color: Colors.white),
            ),
            onPressed: () => {cancelToken?.cancel("设置如此")},
          ),
          MaterialButton(
            color: Colors.indigo,
            child: Text(
              uploadProgress,
              style: const TextStyle(color: Colors.white),
            ),
            onPressed: () => upload(),
          ),
          MaterialButton(
            color: Colors.indigo,
            child: const Text(
              "停止",
              style: TextStyle(color: Colors.white),
            ),
            onPressed: () => cancelTokenUpload?.cancel(),
          ),
        ],
      ),
    );
  }

  request() async {
    var map = HashMap<String, String>();
    map["appType"] = "android";
    map["packageName"] = "com.waterfairy.workcalendar";
//    var response = await Dio().get(url, queryParameters: map);

    HttpRequest<VersionBean>().requestGet(
        "version/getVersionInfo",
        HttpRequestCallback("VersionBean", onSuccess: (value) {
          setState(() {
            downloadUrl = value?.url;
            getContent = jsonEncode(value);
          });
        }, onError: (code, msg) {
        }),
        map);
  }

  download() async {
    if (downloadUrl != null) {
      cancelToken = await dioHttp.requestDown(downloadUrl!, downloadProgressCallback);
    }
  }

  upload() async {
    cancelTokenUpload = CancelToken();
    dioHttp.upload(cancelTokenUpload, uploadProgressCallback);
  }

  downloadProgressCallback(int count, int total) {
    setState(() {
      downloadProgress = "下载$count/$total";
    });
  }

  uploadProgressCallback(int count, int total) {
    setState(() {
      uploadProgress = "上传$count/$total";
    });
  }
}
